﻿using System;
using System.Collections;
using System.Data;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SiteFox.WebParts.Navigation
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]

    public class MenuWP : WebPart
    {
        Menu _Menu = new Menu();

        #region PublicProperies
        private string _CssClass = "";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Enter style class name")]
        [WebDisplayName("CssClass")]
        public new string CssClass
        {
            get { return _CssClass; }
            set { _CssClass = value; }
        }

        protected Orientation _Orientation = Orientation.Horizontal;
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Select menu orientation.")]
        [WebDisplayName("Orientation")]
        public virtual Orientation Orientation
        {
            get { return _Orientation; }
            set { _Orientation = value; }
        }

        private int _MaximumDynamicDisplayLevels = 0;
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Enter max. dynamic display level.")]
        [WebDisplayName("Maximum dynamic display level")]
        public int MaximumDynamicDisplayLevels
        {
            get { return _MaximumDynamicDisplayLevels; }
            set { _MaximumDynamicDisplayLevels = value; }
        }

        private string _StaticTopSeparatorImageUrl = "";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Insert top separator image url.")]
        [WebDisplayName("Top separator image url")]
        public string StaticTopSeparatorImageUrl
        {
            get { return _StaticTopSeparatorImageUrl; }
            set { _StaticTopSeparatorImageUrl = value; }
        }

        private string _StaticBottomSeparatorImageUrl = "";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Insert bottom separator image url.")]
        [WebDisplayName("Bottom separator image url")]
        public string StaticBottomSeparatorImageUrl
        {
            get { return _StaticBottomSeparatorImageUrl; }
            set { _StaticBottomSeparatorImageUrl = value; }
        }

        private bool _StaticEnableDefaultPopOutImage = false;
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("StaticEnableDefaultPopOutImage.")]
        [WebDisplayName("Static enable default PopOut image")]
        public bool StaticEnableDefaultPopOutImage
        {
            get { return _StaticEnableDefaultPopOutImage; }
            set { _StaticEnableDefaultPopOutImage = value; }
        }
        #endregion

        protected override void CreateChildControls()
        {
            Controls.Clear();
            _Menu = new Menu();
            _Menu.CssClass = _CssClass;
            _Menu.DynamicHoverStyle.CssClass = _CssClass + "_DynamicHoverStyle";
            _Menu.DynamicMenuItemStyle.CssClass = _CssClass + "_DynamicMenuItemStyle";
            _Menu.DynamicMenuStyle.CssClass = _CssClass + "_DynamicMenuStyle";
            _Menu.DynamicSelectedStyle.CssClass = _CssClass + "DynamicSelectedStyle";
            _Menu.StaticHoverStyle.CssClass = _CssClass + "StaticHoverStyle";
            _Menu.StaticMenuItemStyle.CssClass = _CssClass + "StaticMenuItemStyle";
            _Menu.StaticMenuStyle.CssClass = _CssClass + "StaticMenuStyle";
            _Menu.StaticSelectedStyle.CssClass = _CssClass + "StaticSelectedStyle";
            _Menu.Orientation = _Orientation;
            _Menu.MaximumDynamicDisplayLevels = _MaximumDynamicDisplayLevels;
            _Menu.StaticTopSeparatorImageUrl = _StaticTopSeparatorImageUrl;
            _Menu.StaticBottomSeparatorImageUrl = _StaticBottomSeparatorImageUrl;
            _Menu.StaticEnableDefaultPopOutImage = _StaticEnableDefaultPopOutImage;
            _Menu.Items.Clear();
            var _RootNodes = SiteFox.Business.Manager.SiteMapNode.GetRootNodes();
            for (int i = 0; i < _RootNodes.Count; i++)
            {
                var _RootNode = _RootNodes[i];
                MenuItem _mi = new MenuItem(_RootNode.Title, _RootNode.ID.ToString(), "", _RootNode.Url, _RootNode.Target);
                this.LoadChildNodes(_mi);
                _Menu.Items.Add(_mi);
            }
            Controls.Add(_Menu);
        }
        void LoadChildNodes(MenuItem _ParentNode)
        {
            var _ChildNodes = SiteFox.Business.Manager.SiteMapNode.GetByParentNode(Convert.ToInt32(_ParentNode.Value));
            for (int i = 0; i < _ChildNodes.Count; i++)
            {
                var _ChildNode = _ChildNodes[i];
                MenuItem _mi = new MenuItem(_ChildNode.Title, _ChildNode.ID.ToString(), "", _ChildNode.Url, _ChildNode.Target);
                this.LoadChildNodes(_mi);
                _ParentNode.ChildItems.Add(_mi);
            }
        }
    }
}
